package server;
import java.util.ArrayList;



public class Database  {
	
	
	private int oprID= 10;
	private ArrayList <User> persons;
		
	public Database(){
		User admin = new User (this.oprID, "Admin", 0101010101, ">02324it!<", 0);
		persons = new ArrayList<User>();
		persons.add(admin);
	}
	
	protected User select  ( int oprID ) {
		int index = -1;
		for (User curData : this.persons) {
			if (curData.getoprID() == oprID) {
				index = persons.indexOf(curData);
			}
		}
		if (index != -1) {
			return persons.get(index);
		}
		return null; 
	 }

	protected boolean add(String oprNavn, int cpr, String pass, int ini) {
		this.oprID++;
		User newPerson= new User (this.oprID, oprNavn, cpr, pass, ini);
		this.persons.add(newPerson);
		return true;
	}
	
	protected boolean update(int oprID, String oprNavn, int cpr, String pass, int ini ) {
		int index = -1;
		for (User curData : this.persons) {
			if (curData.getoprID() == oprID) {
				index = persons.indexOf(curData);
				break;
			}
		}
		User newData = new User (oprID, oprNavn, cpr, pass, ini);
		try {
			persons.set(index, newData); 
		}
		catch (ArrayIndexOutOfBoundsException e) {
			return false;
		}	
		return true;
	}
	
	protected boolean delete  (int oprID, String oprNavn, int cpr, String pass, int ini ) {
		int index = -1;
		for (User curData : this.persons) {
			if (curData.getoprID() == oprID) {
				index = persons.indexOf(curData);
				break;
			}
		}
		if (index == -1) {
			return false;
		}
		persons.remove(index);
		return true;
	}
}
